package com.guigusuqi.app.controller;


import com.guigusuqi.app.entity.DTO.MedicalCardDTO;
import com.guigusuqi.app.entity.LoginWxUser;
import com.guigusuqi.app.entity.MedicalCard;
import com.guigusuqi.app.service.MedicalCardService;
import com.guigusuqi.commonutils.utils.GetTokenByAuthorizationUtils;
import com.guigusuqi.commonutils.utils.RedisCache;
import com.guigusuqi.commonutils.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author suqi
 * @since 2022-04-06
 */
@RestController
@RequestMapping("/yy")
@Api(tags = {"wx-就诊卡管理"})
public class MedicalCardController
{
    @Resource
    private MedicalCardService medicalCardService;

    @Resource
    private RedisCache redisCache;

    @ApiOperation("通过openid查询就诊卡信息")
    @GetMapping("/findMedicalCardList")
    public Result findMedicalCardList() throws IOException
    {
        String token = GetTokenByAuthorizationUtils.getToken();
        LoginWxUser loginWxUser = redisCache.getCacheObject("TOKEN_"+token);
        if (ObjectUtils.isEmpty(loginWxUser))
        {
            return Result.fail().message("用户信息已过期，请重新登录!");
        }
        return medicalCardService.findMedicalCardList(loginWxUser.getUser().getOpenid());
    }

    @ApiOperation("绑定就诊卡")
    @PostMapping("/getMedicalCard")
    public Result getMedicalCard(@RequestBody MedicalCard medicalCard) throws IOException
    {
        String token = GetTokenByAuthorizationUtils.getToken();
        LoginWxUser loginWxUser = redisCache.getCacheObject("TOKEN_"+token);
        if (ObjectUtils.isEmpty(loginWxUser))
        {
            return Result.fail().message("用户信息已过期，请重新登录!");
        }
        return medicalCardService.getMedicalCard(loginWxUser.getUser().getOpenid(),medicalCard);
    }

    @ApiOperation("建立就诊卡")
    @PostMapping("/addMedicalCard")
    public Result addMedicalCard(@RequestBody MedicalCardDTO medicalCardDTO) throws IOException
    {
        String token = GetTokenByAuthorizationUtils.getToken();
        LoginWxUser loginWxUser = redisCache.getCacheObject("TOKEN_"+token);
        if (ObjectUtils.isEmpty(loginWxUser))
        {
            return Result.fail().message("用户信息已过期，请重新登录!");
        }
        return medicalCardService.addMedicalCard(loginWxUser.getUser().getOpenid(),medicalCardDTO);
    }
}

